//
//  DFProductsViewController.m
//  DF
//
//  Created by lanou on 15/10/23.
//  Copyright © 2015年 xuzenghui. All rights reserved.
//

#import "DFProductsViewController.h"
#import "MultilevelTableViewCell.h"
#import "DFProductsRightTableViewController.h"
#import "PKRequestManager.h"
#import "DFProductsSearchController.h"
#import "Reachability.h"
@interface DFProductsViewController ()<UITableViewDelegate,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         UITableViewDataSource,UISearchBarDelegate>
@property (nonatomic,strong)DFProductsDetailViewController *listController;
@property (nonatomic,strong)NSArray *keyArr;
@property (nonatomic,strong)UINavigationController *nav;
@property (nonatomic,strong)UITableView *leftTableView;
@property (nonatomic,strong)UICollectionView *rightTableView;
@property (nonatomic,strong)DFProductsRightTableViewController *rightTVC;
@property (nonatomic,strong)UISearchBar *searchBar;
@property (nonatomic,strong)DFProductsSearchController *searchController;
@property (nonatomic,strong)UITableView *accessoryview;
@property (nonatomic,assign)NSInteger selectedIndex;
@end

@implementation DFProductsViewController

- (void)addSearchBar
{
//  更改搜索栏的背景图片
    _searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 7, kScreenWidth, 30)];
    _searchBar.barStyle = UIBarStyleDefault;
    _searchBar.placeholder = @"请输入产品名称或型号";
    _searchBar.delegate = self;
    _selectedIndex = 0;
    
    for (UIView *subview in _searchBar.subviews)
    {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
        {
            [subview removeFromSuperview];
            break;
        }
    }
// 设置背景图片
    [_searchBar setBackgroundImage:[UIImage imageNamed:@"hongbeijing"]];

//  搜索列表
    _searchController = [[DFProductsSearchController alloc]init];

//  实现搜索列表的PUSH跳转
    __block DFProductsViewController *productVC = self;
  
    _searchController.jumpBlock = ^(NSString *string){
          DFProductsDetailViewController *listController = [[DFProductsDetailViewController alloc]init];
        [productVC.navigationController pushViewController:listController animated:YES];
        listController.differentVa = dfProductsSearchController;
        listController.keyword = string;
    };
    _accessoryview = _searchController.tableView;
    _accessoryview.backgroundColor = [UIColor whiteColor];
    _accessoryview.alpha = 0.0;
 
    [self.view addSubview:_accessoryview];
    [self.topView addSubview:_searchBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
      
    self.tabBarController.tabBar.hidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.topView];
  
    // Do any additional setup after loading the view.
    _keyArr = @[@"手机",@"笔记本电脑",@"平板电脑",@"超级本",@"移动电源",@"无线路由器",@"游戏机",@"掌上游戏机",@"剃须刀",@"台式整机",@"智能穿戴",@"相机摄像机",@"装机硬件",@"数码产品",@"硬件外设"];
//  初始化左侧控制器
    _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kLeftWidth, kScreenHeight - 108)];
    _leftTableView.delegate = self;
    _leftTableView.rowHeight = 46;
    _leftTableView.dataSource = self;
    [_leftTableView registerClass:[MultilevelTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_leftTableView];
//  加载右侧视图
    _rightTVC = [[DFProductsRightTableViewController alloc]init];
    _rightTVC.tableView.frame = CGRectMake(120, 64, kScreenWidth - 120, kScreenHeight - 108);
    self.navigationController.navigationBarHidden = YES;
    _rightTVC.tab = self.tabBarController;
    _rightTVC.nav = self.navigationController;
    [self.view addSubview:_rightTVC.view];
    
 //  添加searchBar
    [self addSearchBar];
 }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _keyArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MultilevelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titile.text = _keyArr[indexPath.row];
//  被选中cell状态改变，其他不变
    if (indexPath.row == _selectedIndex) {
        cell.titile.backgroundColor = [UIColor whiteColor];
        cell.titile.textColor = [UIColor colorWithRed:0.194 green:0.614 blue:1.000 alpha:1.000];
    }else
    {
        cell.titile.backgroundColor = [UIColor colorWithWhite:0.895 alpha:1.000];
        cell.titile.textColor = [UIColor blackColor];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > 8) {
        _rightTVC.lastIndex = indexPath.row;
        [_rightTVC.tableView reloadData];
    }
    else{
         [_rightTVC requestDataWithIndex:indexPath.row];
        _rightTVC.lastIndex = 0;
       }
    if (indexPath.row < 4 && indexPath.row > 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.leftTableView.contentOffset = CGPointMake(0, 46 * indexPath.row);
        }];
}
//  被选中cell传值
    _selectedIndex = indexPath.row;

    [self didseclecedToChangeLeftView:indexPath.row];
}

- (void)controlAccessViewAlpha:(float)alphaValue
{
    [UIView animateWithDuration:0.43 animations:^{
        [self.accessoryview setAlpha:alphaValue];
    } completion:nil];
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    [searchBar setShowsCancelButton:YES animated:YES];
    //  修改取消按钮文字为中文
    for(UIView *view in  [[[_searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
    [self controlAccessViewAlpha:1.0];
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [UIView animateWithDuration:0.43 animations:^{
        [self.searchBar resignFirstResponder];
    } completion:nil];

    [_searchController.dataBaseArr removeAllObjects];
    [_searchController.tableView reloadData];
    searchBar.text = @"";
    [self.searchBar setShowsCancelButton:NO animated:YES];
    [self controlAccessViewAlpha:0];
 }

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self controlAccessViewAlpha:1];
    [_searchController.dataBaseArr removeAllObjects];
    [_searchController.tableView reloadData];
    [_searchController requestSearchDataWithSearchContent:[searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

//  选中状态
- (void)didseclecedToChangeLeftView:(NSInteger)didSeclected
{
    for (int i = 0; i < _keyArr.count; i++) {
        MultilevelTableViewCell *cell =
        (MultilevelTableViewCell*)[self.leftTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        cell.titile.backgroundColor = [UIColor colorWithWhite:0.895 alpha:1.000];
        cell.titile.textColor = [UIColor blackColor];
    }
    MultilevelTableViewCell *cell =
    (MultilevelTableViewCell*)[self.leftTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:didSeclected inSection:0]];
    cell.titile.backgroundColor = [UIColor whiteColor];
    cell.titile.textColor = [UIColor colorWithRed:0.194 green:0.614 blue:1.000 alpha:1.000];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
